package view;

import java.text.SimpleDateFormat;

import control.GameControl;
/**
 * This class is responsible for keeping track 
 * of the gametime. (by using an extra thread)
 * 
 * @author Thomas
 *
 */

public class TimerThread extends Thread
{
	private SimpleDateFormat gameTimeFormat = new SimpleDateFormat("mm:ss");
	private long gameTime;
	
	public TimerThread(long _gameTime)
	{
		this.gameTime = _gameTime;
	}
	
	public void run()
	{
		long time = gameTime;
		for(long i = time; i >= 0; i--)
		{
			if(i == 0)
			{
				GameControl.getControlInstance().setGameTime(i);
				GameControl.getControlInstance().endGame();
			}
			else
			{
				try 
				{
					Thread.sleep(1000);
				} 
				catch (InterruptedException e) 
				{
					GameControl.getControlInstance().setGameTime(this.gameTime);
					return;
				}
				
				GameControl.getControlInstance().getBoard().updateGameTime(gameTimeFormat.format(--gameTime*1000));
			}
				
		}
	}

}
